package com.example.actearly_revised;

import android.os.Bundle;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.text.style.TextAppearanceSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableLayout.LayoutParams;
import android.widget.TableRow;
import android.widget.TextView;

public class PickProfile extends Activity {

	Intent p_intent;
	String selectedMode;
	View actionbarView;
	boolean noUsers;
	TextView message;
	TableLayout layout;
	Button add;
	int currentProfile;
	Button[] profiles;
	int[] ID;

	@TargetApi(16)
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pick_profile);

		LinearLayout pageLayout=(LinearLayout)findViewById(R.id.ProfileLayout);
		//pageLayout.setgroundResource(R.drawable.background);

		//change font/color/text in action bar
		Typeface customFont = Typeface.createFromAsset(getAssets(), "fonts/futuram.ttf");
		getActionBar().setTitle("Pick a child's profile");

		p_intent = getIntent();
		selectedMode = p_intent.getStringExtra("ModeSelected");
		noUsers = p_intent.getBooleanExtra("NoUsers", false);

		message = (TextView) findViewById(R.id.txtProfileMessage);
		// message.setPadding(20, 20, 20, 20);
		message.setTypeface(customFont);

		layout = (TableLayout) findViewById(R.id.Profiles);
		add = (Button) findViewById(R.id.bttnAdd);

		if(selectedMode.equals("SelectProfile")){
			message.setText("Select a child's profile to track his or her milestones:");
		}
		else {
			message.setText("Select a child's profile to edit or add a new profile:");
		}

		User[] allUsers;
		SQLogic conn = new SQLogic(PickProfile.this);
		conn.open();
		allUsers = conn.getAllUserData();
		conn.close();

		if(selectedMode.equals("SelectProfile")){
			add.setVisibility(View.INVISIBLE);
		}

		add.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
				Intent intent = new Intent("com.example.actearly_revised.LoginView");
				intent.putExtra("addselected", true);
				intent.putExtra("NoUsers", noUsers);
				startActivity(intent);
			}
		});


		View.OnClickListener genericButton = new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				for(int i = 0; i < profiles.length; i++){
					if(v.equals(profiles[i])){
						currentProfile = ID[i];
						break;
					}
				}
				System.out.println(currentProfile);

				if(selectedMode.equals("AddEditProfile")){
					finish();
					Intent intent = new Intent("com.example.actearly_revised.LoginView");
					intent.putExtra("addselected", false);
					intent.putExtra("profile_number", currentProfile);
					intent.putExtra("NoUsers", noUsers);
					startActivity(intent);
				}
				else{  // want to select profile
					finish();
					Intent intent = new Intent("com.example.actearly_revised.PreTracker");
					intent.putExtra("profile_number", currentProfile);
					startActivity(intent); // we'll edit this part when we have more sceens
				}
			}
		};

		profiles = new Button[allUsers.length];
		ID = new int[allUsers.length];
		// make table rows in table layout for all user profiles in "allUsers"
		for(int i = 0; i < allUsers.length; i++){
			TableRow row = new TableRow(PickProfile.this);
			//row.setPadding(30, 20, 20, 30);
			row.setGravity(Gravity.CENTER_HORIZONTAL);
			row.setPadding(0, 15, 0, 15);

			TextView txt = new TextView(PickProfile.this);
			//txt.setTextSize(20);
			//txt.setPadding(0, 0, 100, 30);
			//txt.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
			txt.setGravity(Gravity.CENTER_HORIZONTAL);

			txt.setTypeface(customFont);
			TextView buttonText= (Button) findViewById(R.id.bttnAdd);
			buttonText.setTypeface(customFont);
			//  txt.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));

			Button bttn = new Button(PickProfile.this);
			bttn.setGravity(Gravity.CENTER_HORIZONTAL);
			//bttn.setTextAppearance(getApplicationContext(), R.style.ButtonStyle);
			if(selectedMode.equals("SelectProfile")){
				//txt.setText(allUsers[i].childname + "\'s \nMilestones");
				bttn.setText(allUsers[i].childname);
				bttn.setTypeface(customFont);

			}
			else {
				//txt.setText(allUsers[i].childname + "\'s \nProfile");
				bttn.setText(allUsers[i].childname);
				bttn.setTypeface(customFont);
			}	       	

			TextView empty = new TextView(PickProfile.this);
			empty.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, .8f));

			profiles[i] = bttn;   
			ID[i] = allUsers[i].childId;
			bttn.setOnClickListener(genericButton);
			row.addView(txt);
			row.addView(bttn);
			row.addView(empty);

			layout.addView(row);

		}
		// each row consists of child's name and button (the button text will depend on chosen mode)
		// set currentProfile depending on which button it is, route all button listiners to "genericbutton"

	}





	/*    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_pick_profile, menu);
        return true;
    }  */
}
